mergeInto(LibraryManager.library, {
    OpenPhotoPicker: function (multiple) {
        // 创建隐藏的file input元素
        var input = document.createElement('input');
        input.type = 'file';
        input.accept = 'image/*';
        input.multiple = multiple === 1;
        input.style.display = 'none';

        // 添加到文档中
        document.body.appendChild(input);

        // 设置change事件处理
        input.onchange = function (event) {
            var files = event.target.files;
            if (!files || files.length === 0) {
                // 没有选择文件，通知Unity
                unityInstance.SendMessage('PhotoManager', 'OnFilesSelected', '');
                return;
            }

            // 准备文件信息数组
            var fileInfos = [];
            
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                
                // 创建对象URL作为临时访问地址
                var objectUrl = URL.createObjectURL(file);
                fileInfos.push({
                    name: file.name,
                    size: file.size,
                    type: file.type,
                    url: objectUrl,
                    lastModified: file.lastModified
                });
            }
            
            // 将文件信息数组转换为JSON字符串传递给Unity
            unityInstance.SendMessage('GetPhotoWeb', '_OnPhotoPickedCallback', JSON.stringify(fileInfos));
            
            // 清理临时创建的input元素
            document.body.removeChild(input);
            
            // 刷新浏览器焦点，以便恢复点击事件
            const canvas = document.querySelector('#unity-canvas');
            // 短暂延迟后重新聚焦
            setTimeout(() => {
                console.log('先让Canvas失去焦点')
                canvas.blur();
                setTimeout(() => {
                    canvas.focus();
                    console.log('重新Canvas获取焦点')
                    // 触发鼠标事件来唤醒Unity事件系统
                    const rect = canvas.getBoundingClientRect();
                    const events = ['mousemove', 'mousedown', 'mouseup', 'click'];
                    events.forEach((eventType, index) => {
                        setTimeout(() => {
                            const event = new MouseEvent(eventType, {
                                view: window,
                                bubbles: true,
                                cancelable: true,
                                clientX: rect.left + 10,
                                clientY: rect.top + 10
                            });
                            canvas.dispatchEvent(event);
                        }, index * 10);
                    });
                }, 100);
            }, 100);
        };

        // 模拟点击以打开文件选择对话框
        input.click();
    },

    RevokeObjectURL: function (url) {
        // 释放之前创建的Object URL
        if (url && url !== '') {
            URL.revokeObjectURL(UTF8ToString(url));
        }
    },
    
    //刷新焦点，以便恢复点击系统
    RefreshFocus: function () {
        setTimeout(() => {
            console.log('先让Canvas失去焦点')
            canvas.blur();
            setTimeout(() => {
                canvas.focus();
                console.log('重新Canvas获取焦点')
                // 触发鼠标事件来唤醒Unity事件系统
                const rect = canvas.getBoundingClientRect();
                const events = ['mousemove', 'mousedown', 'mouseup', 'click'];
                events.forEach((eventType, index) => {
                    setTimeout(() => {
                        const event = new MouseEvent(eventType, {
                            view: window,
                            bubbles: true,
                            cancelable: true,
                            clientX: rect.left + 10,
                            clientY: rect.top + 10
                        });
                        canvas.dispatchEvent(event);
                    }, index * 10);
                });
            }, 100);
        }, 100);
    },
});